package com.wy2.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.wy2.entity.Clock;
import com.wy2.mapper.ClockMapper;
import com.wy2.service.ClockService;
import com.wy2.util.FenyeUtil;

import lombok.Data;

@RestController
@RequestMapping("/userclocks")
@CrossOrigin
@EnableScheduling
public class ClockController {

	@Autowired
	private ClockService ucs;

	@Autowired
	private ClockMapper cm;

	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * 登录成功后查看今天的该用户是否打卡
	 */
	@RequestMapping("/getUsers")
	public Map<String, Object> getCheUsers(String uid) {
		Map<String, Object> map = new HashMap<>();
		String checkintime = sdf.format(new Date());
		System.out.println(checkintime);
		Clock uc = cm.getUsers(uid, checkintime);
		if (uc != null) {
			map.put("code", uc.getCstatus());
			return map;
		} else {
			map.put("code", 0);
			return map;
		}
	}

	/**
	 * 添加打卡用户
	 */
	@RequestMapping("/addClock")
	public Integer addUserClocks(Clock u) {
		return ucs.addClock(u);

	}

	/**
	 * 修改签退时间和状态
	 */
//	@RequestMapping("/upUserClocks")
//	public Integer upUserClocks(Clock u) {
//		System.out.println("-----con"+u);
//		return ucs.upClock(u);
//		
//	}

	@RequestMapping("/upUserClocks")
	public Integer upUserClocks(Clock u, String uid) {
		try {
			u.setExt(uid);
			List<Clock> clist = ucs.getTimelast(u);
			u.setCid(clist.get(0).getCid());
			String intime = clist.get(0).getCheckintime();
			Clock clock = clist.get(0);
			u.setCid(clock.getCid());
			Date date = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			u.setCheckoutime(dateFormat.format(date));
			u.setCstatus("已签退");
			String time = expriredDate(intime);
			System.out.println(",,,,"+time);
			try {
				Boolean a = isTimeRange(intime, time);
				if (a) {
					return 2;
				} else {
					return ucs.upClock(u);
				}
			} catch (ParseException e) {
				e.printStackTrace();
				return 2;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return 2;
		}
	}

	private Boolean isTimeRange(String intime, String time) throws ParseException {
		System.out.println("----"+intime);
		System.out.println("tttttt---"+time);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date now = df.parse(df.format(new Date()));
		Date begin = df.parse(intime);
		Date end = df.parse(time);
		Calendar nowTime = Calendar.getInstance();
		nowTime.setTime(now);
		Calendar beginTime = Calendar.getInstance();
		beginTime.setTime(begin);
		Calendar endTime = Calendar.getInstance();
		endTime.setTime(end);
		if (nowTime.before(endTime) && nowTime.after(beginTime)) {
			return true;
		} else {
			return false;
		}
	}

	private String expriredDate(String dataStr) {
		String aDataStr = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = sdf.parse(dataStr);
			date.setTime(date.getTime() + 1 * 60 * 1000);
			aDataStr = sdf.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return aDataStr;
	}

	/**
	 * 显示所有+分页
	 */
	@RequestMapping("/getAllUclocks")
	@ResponseBody
	public FenyeUtil<Clock> getAllUchecks(FenyeUtil<Clock> fenye, Clock Clock) {
		fenye.setT(Clock);
		fenye = ucs.getAllUchecks(fenye);
		return fenye;
	}
	
	
	
}
